package com.djdevs.calculatorconverter;

//import javax.xml.parsers.*;
//import org.xml.sax.*;
//import org.xml.sax.helpers.*;
import org.xmlpull.v1.XmlPullParserException;

import java.util.*;
//import android.app.Activity;
import android.content.res.XmlResourceParser;
//import android.net.*;

import java.io.*;

public class ReadXml {
	
	//List to hold Employees object
    private List<Converter> lstConverter = null;
    private Converter conv = null;
    private ConvUnit cnvUnit = null;
    
    public ReadXml() {
	}
	
	public ReadXml(XmlResourceParser xrpConverter) {
		ParseXml(xrpConverter);
	}
	
	public List<Converter> getConverterList() {
        return lstConverter;
    }
	
	public ArrayList<String> GetConverterList()
	{
		try {
			ArrayList<String> converterList = new ArrayList<String>();
			for (int i = 0; i < this.lstConverter.size(); i++) {
				Converter conv = this.lstConverter.get(i);
				converterList.add(conv.type);
			}
			
			return converterList;
		}
		catch (Exception e)
		{
			return new ArrayList<String>();
		}
	}
	
	public ArrayList<String> GetUnitListForConverter(String categoryType)
	{
		try {
			ArrayList<String> unitList = new ArrayList<String>();
			Converter convForUnit = null;
			
			for (int i = 0; i < this.lstConverter.size(); i++) {
				if (this.lstConverter.get(i).type.equals(categoryType)) {
					convForUnit = this.lstConverter.get(i);
					break;
				}
			}
			
			if (convForUnit != null) {
//				Collections.sort(convForUnit.lstUnits, new Comparator<ConvUnit>() {
//				    @Override
//				    public int compare(ConvUnit unit1, ConvUnit unit2) {
//				        if (unit1.conversionFactor > unit2.conversionFactor)
//				            return 1;
//				        if (unit1.conversionFactor < unit2.conversionFactor)
//				            return -1;
//				        return 0;
//				    }
//				});
				for (int i = 0; i < convForUnit.lstUnits.size(); i++) {
					unitList.add(convForUnit.lstUnits.get(i).unitName + " ( " + convForUnit.lstUnits.get(i).unitCode + " )"); 
					//unitList.add(convForUnit.lstUnits.get(i).unitName);
				}
			}
			
			return unitList;
		}
		catch (Exception e)
		{
			return new ArrayList<String>();
		}
	}
	
	public Converter GetConverterForCategory(String categoryType)
	{
		try {
			Converter convForUnit = new Converter();
			
			for (int i = 0; i < this.lstConverter.size(); i++) {
				if (this.lstConverter.get(i).type.equals(categoryType)) {
					convForUnit = this.lstConverter.get(i);
					break;
				}
			}
			return convForUnit;
		}
		catch (Exception e)
		{
			return new Converter();
		}
	}
	
	public String GetBaseConverterUnitString(String categoryType)
	{
		ConvUnit baseConvUnit = GetBaseConverterUnit(categoryType);
		return baseConvUnit.unitName + " ( " + baseConvUnit.unitCode + " )";
	}
	
	public ConvUnit GetBaseConverterUnit(String categoryType)
	{
		ConvUnit baseConvUnit = new ConvUnit();
		Converter convForUnit = null;
		
		for (int i = 0; i < this.lstConverter.size(); i++) {
			if (this.lstConverter.get(i).type.equals(categoryType)) {
				convForUnit = this.lstConverter.get(i);
				break;
			}
		}
		
		if (convForUnit != null) {
			for (int i = 0; i < convForUnit.lstUnits.size(); i++) {
				if (convForUnit.baseUnitCode.equalsIgnoreCase(convForUnit.lstUnits.get(i).unitCode)) {
					baseConvUnit = convForUnit.lstUnits.get(i);
					break;
				}
			}
		}
		
		return baseConvUnit;
	}
	
	public ConvUnit GetConverterUnitForCategoryType(Converter converter, String convUnitText)
	{
		ConvUnit unit = new ConvUnit();
		String convUnitName = convUnitText.substring(0, convUnitText.indexOf("(")).trim();
		String convUnitCode = convUnitText.substring(convUnitText.indexOf("(") + 1, convUnitText.indexOf(")")).trim();
		
		if (converter != null) {
			for (int i = 0; i < converter.lstUnits.size(); i++) {
				if (convUnitCode.equalsIgnoreCase(converter.lstUnits.get(i).unitCode) 
						&& convUnitName.equalsIgnoreCase(converter.lstUnits.get(i).unitName)) {
					unit = converter.lstUnits.get(i);
					break;
				}
			}
		}
		return unit;
	}
	
	private void ParseXml(XmlResourceParser xrpConverter)
	{
		int eventType = -1;
		try
		{
			while(eventType != XmlResourceParser.END_DOCUMENT)
	    	{
	            if (eventType == XmlResourceParser.START_TAG) {
	                String tagName = xrpConverter.getName();
	                if (tagName.equals("Converters")) {
	                	lstConverter = new ArrayList<Converter>();
	                }
	                else if (tagName.equals("Converter")) {
	                	conv = new Converter();
	                	conv.type = xrpConverter.getAttributeValue(1);
	                	conv.baseUnitCode = xrpConverter.getAttributeValue(2);
	                	conv.lstUnits = new ArrayList<ConvUnit>();
					}
	                else if (tagName.equals("Unit")) {
	                	cnvUnit = new ConvUnit();
	                	cnvUnit.id = Integer.parseInt(xrpConverter.getAttributeValue(0));
	                	cnvUnit.unitCode = xrpConverter.getAttributeValue(1);
	                	cnvUnit.unitName = xrpConverter.getAttributeValue(2);
	                	cnvUnit.conversionFactor = Double.parseDouble(xrpConverter.getAttributeValue(3));
	                	if (conv.type.equalsIgnoreCase("temperature")) 
	                	{
							cnvUnit.defaultZero = Double.parseDouble(xrpConverter.getAttributeValue(5));
						}	
	                	conv.lstUnits.add(cnvUnit);
					}
	            }
	            else if (eventType == XmlResourceParser.END_TAG) {
	            	String tagName = xrpConverter.getName();
	                if (tagName.equals("Converter")) {
	                	lstConverter.add(conv);
					}
				}
	            
	            xrpConverter.next();
	            eventType = xrpConverter.getEventType();
	    	}
		}
	    catch (XmlPullParserException e) {
	        //e.printStackTrace();
	    }
		catch (IOException e) {
			//e.printStackTrace();
		} 
		catch (Exception e) {
			//e.printStackTrace();
		} 
	}
}